<?php

namespace Application\Model\DBO;

use Admin\Model\DBO\SearchBookDBO;
use Zend\Db\TableGateway\AbstractTableGateway;

class BookingOnlineDBO extends AbstractTableGateway {

    private $sm;
    private $orderOnlineTable;

    public function __construct($serviceManager) {
        $this->sm = $serviceManager;
    }

    public function getOrderOnlineTable() {
        if (!$this->orderOnlineTable) {
            $this->orderOnlineTable = $this->sm->get('Admin\Model\DAO\OrderOnlineTable');
        }
        return $this->orderOnlineTable;
    }
    public function getBookOrder($cardBarcode) {
        $resultSet = $this->getOrderOnlineTable()->getBookOrder($cardBarcode);
        $objectSearchBook = new SearchBookDBO($this->sm);
        // Tim kiếm tác gả dựa vào mã sách
        $allAuthor = array();
        foreach ($resultSet as $key) {
            $bookBarcodeTmp = $key['bookBarcode'];
            $stringAuthor = "";
            $authors = $objectSearchBook->searchBookAuthor($bookBarcodeTmp);
            foreach ($authors as $author) {
                $stringAuthor .= $author['author'] . '-';
            }
            $allAuthor[$bookBarcodeTmp] = $stringAuthor;
        }
        $iteratorAdapter = new \Zend\Paginator\Adapter\Iterator($this->getOrderOnlineTable()->getBookOrder($cardBarcode));
        $bookOrder = new \Zend\Paginator\Paginator($iteratorAdapter);
        return array(
            'bookOrder' => $bookOrder,
            'allAuthor' => $allAuthor,
            );
    }
    public function getAllBookOrder() {
        $resultSet = $this->getOrderOnlineTable()->fetchAll();
        $iteratorAdapter = new \Zend\Paginator\Adapter\Iterator($resultSet);
        $allBookOrder = new \Zend\Paginator\Paginator($iteratorAdapter);
        return $allBookOrder;
    }

    public function OrderBook($cardBacode, $bookID) {
        // Neu sach chua dang ky thi duoc dang ky con  khong tra ve sai
        if (!$this->getOrderOnlineTable()->getBookID($bookID)) {
            $date = date('Y-m-d');
            $data = array(
                'dateOrder' => $date,
                'orderStatus' => 0,
                'cardBarcode' => $cardBacode,
                'bookID' => $bookID,
            );
            $this->getOrderOnlineTable()->saveData($data);
            return true;
        }
        return false;
    }
    public function applyBorrowBookOnline($bookID) {
        if($this->getOrderOnlineTable()->getBookID($bookID)) {
            $this->getOrderOnlineTable()->updateOrderStatus($bookID, 1);
            return true;
        }
        return false;
        
    }
     public function denyBorrowBookOnline($bookID) {
        if($this->getOrderOnlineTable()->getBookID($bookID)) {
            $this->getOrderOnlineTable()->updateOrderStatus($bookID, -1);
            return true;
        }
        return false;
        
    }
     public function deleteBorrowBookOnline($bookID) {
        if($this->getOrderOnlineTable()->getBookID($bookID)) {
            $this->getOrderOnlineTable()->deleteOrderBook($bookID);
            return true;
        }
        return false;
        
    }
}